﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for PasswordRequirement
/// </summary>
public class PasswordRequirement
{
	public PasswordRequirement()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static int getpasswordlength (string password)
    {

        int passwordlength;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select PasswordLength  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strpasswordlength = command.ExecuteScalar().ToString();
             passwordlength = Convert.ToInt32(strpasswordlength);
            sqlConnection.Close();



            int countlength = 0;

            foreach (char c in password)
            {
               
                    countlength++;
               

            }



            //Check password length comparison count
            if (countlength < passwordlength)
            {
               return passwordlength ; // Password length num retrieve

            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return passwordlength;

    }



    public static int getupperchar(string password)
    {

        int upperchar;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinUpperCase  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strpasswordlength = command.ExecuteScalar().ToString();
            upperchar = Convert.ToInt32(strpasswordlength);
            sqlConnection.Close();



            int countlength = 0;

            foreach (char c in password)
            {
                if (char.IsUpper(c))
                {
                    countlength++;
                }

            }



            //Check uppercase comparison count
            if (countlength < upperchar)
            {
                return upperchar; // Password num upperchar retrieve

            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return upperchar;

    }


    public static int getlowerchar(string password)
    {

        int lowerchar;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinLowerCase  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strlowerchar = command.ExecuteScalar().ToString();
            lowerchar = Convert.ToInt32(strlowerchar);
            sqlConnection.Close();



            int countlowerchar = 0;

            foreach (char c in password)
            {
                if (char.IsLower(c))
                {
                    countlowerchar++;
                }

            }



            //Check lowercase comparison count
            if (countlowerchar < lowerchar)
            {
                return lowerchar; // Password num lowerchar retrieve

            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return lowerchar;

    }


    public static int getspecialchar(string password)
    {

        int specialchar;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinSpecialChar  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strspecialchar = command.ExecuteScalar().ToString();
            specialchar = Convert.ToInt32(strspecialchar);
            sqlConnection.Close();



            int countspecialchar = 0;

            foreach (char c in password)
            {
                if (!char.IsLetterOrDigit(c))
                {
                    countspecialchar++;
                }

            }



            //Check specialchar comparison count
            if (countspecialchar < specialchar)
            {
                return specialchar; // Password num specialchar retrieve

            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return specialchar;

    }













    public static bool  validpasswordlength (string password )
    {

        bool ispasswordlen = false;
       

        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select PasswordLength  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);

        
           sqlConnection.Open();
           String strpasswordlength = command.ExecuteScalar().ToString();
           int passwordlength = Convert.ToInt32(strpasswordlength);
           sqlConnection.Close();

           

           int countlength = 0;

           foreach (char c in password)
           {
               
                   countlength++;
               

           }



            //Check password length
           if (countlength < passwordlength  )
           {
               ispasswordlen = false; // Password length too short
               
           }

           else
           {
               ispasswordlen = true; //Password length is fine
               return ispasswordlen;
           }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return ispasswordlen;
        
   }




    public static bool validuppercase(string password)
    {

        bool isuppercase = false;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinUpperCase  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String struppercase = command.ExecuteScalar().ToString();
            int minuppercase = Convert.ToInt32(struppercase);
            sqlConnection.Close();



            int countuppercase = 0;

            foreach (char c in password)
            {
                if (char.IsUpper(c))
                {
                    countuppercase++;
                }

            }



            //Check password uppercase
            if (countuppercase < minuppercase)
            {
                isuppercase = false; // Password does not contain uppercase char.

            }

            else
            {
                isuppercase = true; //Password contain uppercase .
                return isuppercase;
            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return isuppercase;

    }


    public static bool validlowercase(string password)
    {

        bool islowercase = false;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinLowerCase  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strlowercase = command.ExecuteScalar().ToString();
            int minlowercase = Convert.ToInt32(strlowercase);
            sqlConnection.Close();



            int countlowercase = 0;

            foreach (char c in password)
            {
                if (char.IsLower(c))
                {
                    countlowercase++;
                }

            }



            //Check password lowercase 
            if (countlowercase < minlowercase)
            {
                islowercase = false; // Password does not contain lowercase char.

            }

            else
            {
                islowercase = true; //Password contain lowercase .
                return islowercase;
            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return islowercase;

    }


    public static bool validspecialchar(string password)
    {

        bool isspecialchar = false;


        try
        {

            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);

            String strSelect = "Select MinSpecialChar  from [GlobalSetting] ";

            SqlCommand command = new SqlCommand(strSelect, sqlConnection);


            sqlConnection.Open();
            String strspecialchar = command.ExecuteScalar().ToString();
            int minspecialchar = Convert.ToInt32(strspecialchar);
            sqlConnection.Close();



            int countspecialchar = 0;

            foreach (char c in password)
            {
                if (!char.IsLetterOrDigit(c))
                {
                    countspecialchar++;
                }

            }



            //Check password specialchar 
            if (countspecialchar < minspecialchar)
            {
                isspecialchar = false; // Password does not contain special char.

            }

            else
            {
                isspecialchar = true; //Password contain special char .
                return isspecialchar;
            }

        }


        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }

        return isspecialchar;

    }





}